package com.engr.lyx.blog.biz.controller;

import com.engr.lyx.blog.biz.service.ArticleService;
import com.engr.lyx.blog.client.dto.ArticleDTO;
import com.engr.lyx.common.annotation.FreeAccess;
import com.engr.lyx.common.base.ResultBody;
import com.engr.lyx.common.security.UserHelper;
import com.engr.lyx.common.security.UserProfileDetails;
import com.engr.lyx.common.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@Api(tags = "Article apis")
@RestController
@RequestMapping(value = "/api/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @ApiOperation(value = "根据Id查询文章", notes = "根据Id查询文章")
    @PostMapping(value = "/detail")
    @FreeAccess
    public ResultBody selectArticleListByUser(@RequestBody ArticleDTO articleDto) {

        return ResultUtils.success(articleService.selectById(articleDto));
    }

    @ApiOperation(value = "查找浏览量最多的文章", notes = "查找浏览量最多的文章")
    @GetMapping(value = "/view/list")
    @FreeAccess
    public ResultBody selectTopArticleByUserId() {

        UserProfileDetails userProfileDetails = UserHelper.getUserAuth();
        return ResultUtils.success(articleService.selectTopArticleVisit(userProfileDetails == null ? 1L : userProfileDetails.getUserId()));
    }

    @ApiOperation(value = "用户所有文章及文章详情列表", notes = "博客首页列表")
    @GetMapping(value = "/list")
    @FreeAccess
    public ResultBody findUserArticleInfoList(@RequestParam(required = false, defaultValue = "0") Integer pageNum) {

        // 主页最多显示10条
        Pageable pageable = PageRequest.of(pageNum, 10, Sort.Direction.DESC, "id");
        UserProfileDetails userProfileDetails = UserHelper.getUserAuth();
        return ResultUtils.success(articleService.selectByUserId(userProfileDetails == null ? 1L : userProfileDetails.getUserId(), pageable));
    }

    @ApiOperation(value = "根据标签类型查找文章", notes = "根据标签类型查找文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagId", required = true, value = "标签Id", paramType = "path")
    })
    @GetMapping(value = "/{tagId}/list")
    @FreeAccess
    public ResultBody findByTagId(@PathVariable(value = "tagId") Integer tagId, @RequestParam(required = false, defaultValue = "0") Integer page,
                                  @RequestParam(required = false, defaultValue = "20") Integer size) {


        UserProfileDetails userProfileDetails = UserHelper.getUserAuth();
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "id");
        return ResultUtils.success(articleService.selectArticlesByTagId(tagId, userProfileDetails == null ? 1L : userProfileDetails.getUserId(), pageable));
    }

    @ApiOperation(value = "查找点赞数最多的文章", notes = "查找点赞数最多的文章")
    @GetMapping(value = "/hottest/list")
    @FreeAccess
    public ResultBody findArticleOrderByTags2() {

        UserProfileDetails userProfileDetails = UserHelper.getUserAuth();
        return ResultUtils.success(articleService.selectAllByOrderByTags2Desc(userProfileDetails == null ? 1L : userProfileDetails.getUserId()));
    }

    @ApiOperation(value = "根据id查找上下两篇文章", notes = "根据id查找上下两篇文章")
    @GetMapping(value = "/{id}/near")
    @FreeAccess
    public ResultBody findArticleByIdNear(@PathVariable Long id) {

        UserProfileDetails userProfileDetails = UserHelper.getUserAuth();
        return ResultUtils.success(articleService.selectArticleByIdNear(id, userProfileDetails == null ? 1L : userProfileDetails.getUserId()));
    }

    @ApiOperation(value = "查询文章归档", notes = "查询文章归档")
    @GetMapping(value = "/archive")
    @FreeAccess
    public ResultBody findArticleArchive() {

        return ResultUtils.success(articleService.selectByArchive());
    }

}
